package com.example.qixin0314.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.qixin0314.Nat.BaseApplication;
import com.example.qixin0314.Nat.homenewsdata.Values3;
import com.example.qixin0314.Nat.homenewsdata.homenewsdata;
import com.example.qixin0314.Nat.pictureModelData.ValuesP;
import com.example.qixin0314.Nat.pictureModelData.picturebine;
import com.example.qixin0314.Nat.titleModelData.Responsedata;
import com.example.qixin0314.Nat.titleModelData.ValuesBody;
import com.example.qixin0314.R;
import com.example.qixin0314.Activity.SelectActivity;
import com.example.qixin0314.bean.newsAdapter;
import com.example.qixin0314.bean.newsModel;
import com.example.qixin0314.bean.recycleAdapter;
import com.example.qixin0314.bean.recyclePictureAdapter;
import com.example.qixin0314.bean.recyclePictureModel;
import com.example.qixin0314.bean.recycleTextModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class homeFragment extends Fragment {
    private static final String TAG = homeFragment.class.getSimpleName();
    private TextView textSelect;
    private Intent intent;
    private ImageView selectPinpai;
    private ImageView selectHangye;
    private ImageView selectQiye;
    private RecyclerView recycleText;
    private TextView more_pinpai;
    private RecyclerView recycle_image;
    private RecyclerView recycle_news;
    private TextView more_news;
    private onItemViewClickListener listener;

    public void setListener(onItemViewClickListener listener) {
        this.listener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        initView(root);
        clockSelectActivity();//搜索框点击之后跳转
        clockSelectPinpai();//搜品牌图标点击跳转
        clockSelectHangye();//搜行业图标点击跳转
        clockSelectQiye();//搜企业图标点击跳转
        Amore_pinpai();//回调接口
        recycleTextStyle();//设置recycleText为水平并添加数据
        recyclePictureStyle();//设置recycleText为水平并添加数据
        recycle_newsStyle();//设置recycle_news为竖直并添加数据
        return root;
    }

    /**
     * 控件初始化
     */
    private void initView(View root) {
        textSelect = root.findViewById(R.id.text_select);
        selectPinpai = root.findViewById(R.id.select_pinpai);
        selectHangye = root.findViewById(R.id.select_hangye);
        selectQiye = root.findViewById(R.id.select_qiye);
        recycleText = root.findViewById(R.id.recycle_text);
        more_pinpai = root.findViewById(R.id.more_pinpai);
        recycle_image = root.findViewById(R.id.recycle_image);
        recycle_news=root.findViewById(R.id.recycle_news);
        more_news=root.findViewById(R.id.more_news);

        intent = new Intent(requireActivity(), SelectActivity.class);

    }

    private void recycle_newsStyle() {
        LinearLayoutManager llm = new LinearLayoutManager(getContext());
        recycle_news.setLayoutManager(llm);
        List<newsModel>newsData=new ArrayList<>();
        newsAdapter adapter=new newsAdapter(getActivity());
        adapter.setNewsData(newsData);
        recycle_news.setAdapter(adapter);
        Call<homenewsdata<Values3>> homenews = BaseApplication.httpbinService.homenews();
        homenews.enqueue(new Callback<homenewsdata<Values3>>() {
            @Override
            public void onResponse(Call<homenewsdata<Values3>> call, Response<homenewsdata<Values3>> response) {
                for (int i = 0; i < response.body().getValues().getTui_baodao_news().size(); i++) {
                    newsModel item=new newsModel();
                    item.setUrl(response.body().getValues().getTui_baodao_news().get(i).getUrl());
                    item.setTitle(response.body().getValues().getTui_baodao_news().get(i).getTitle());
                    item.setTime(response.body().getValues().getTui_baodao_news().get(i).getAdd_time());
                    item.setId(response.body().getValues().getTui_baodao_news().get(i).getId());
                    newsData.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<homenewsdata<Values3>> call, Throwable t) {

            }
        });


    }

    /**
     * 搜索框点击之后跳转
     */
    private void clockSelectActivity() {
        textSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }

    /**
     * 搜品牌图标点击跳转
     */
    private void clockSelectPinpai() {
        selectPinpai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }

    /**
     * 搜行业图标点击跳转
     */
    private void clockSelectHangye() {
        selectHangye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }

    /**
     * 搜企业图标点击跳转
     */
    private void clockSelectQiye() {
        selectQiye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }

    /**
     * recycleText横向设置,并添加数据
     */
    private void recycleTextStyle() {
        //默认为垂直
        LinearLayoutManager llm = new LinearLayoutManager(requireActivity());
        //设为水平
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycleText.setLayoutManager(llm);//设置他的展现形式
        //2.构建条目列表
        //3.构建数据模型
        //4.创建adapter
        //5.添加数据
        List<recycleTextModel> data = new ArrayList<>();

        recycleAdapter adapter = new recycleAdapter(requireActivity());
        adapter.setData(data);
        recycleText.setAdapter(adapter);

        Call<Responsedata<ValuesBody>> call = BaseApplication.httpbinService.recycleText("android");
        call.enqueue(new Callback<Responsedata<ValuesBody>>() {
            @Override
            public void onResponse(Call<Responsedata<ValuesBody>> call, Response<Responsedata<ValuesBody>> response) {

                for (int i = 0; i < response.body().getValues().getData().size(); i++) {
                    recycleTextModel item = new recycleTextModel();
                    item.setName(response.body().getValues().getData().get(i).getTitle());
                    item.setUrl(response.body().getValues().getData().get(i).getUrl_wap());
                    item.setId(response.body().getValues().getData().get(i).getMapping_id());
                    data.add(item);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<Responsedata<ValuesBody>> call, Throwable t) {
            }
        });

    }

    /**
     * recycleText横向设置,并添加图片
     */
    private void recyclePictureStyle() {
        //默认为垂直
        LinearLayoutManager llm = new LinearLayoutManager(requireActivity());
        //设为水平
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycle_image.setLayoutManager(llm);//设置他的展现形式

        List<recyclePictureModel> list = new ArrayList<>();
        recyclePictureAdapter adapter1 = new recyclePictureAdapter(getContext());
        adapter1.setList(list);
        recycle_image.setAdapter(adapter1);

        //2.构建条目列表
        //3.构建数据模型
        //4.创建adapter
        //5.添加数据

        Call<picturebine<ValuesP>> call = BaseApplication.httpbinService.recyclePicture();
        call.enqueue(new Callback<picturebine<ValuesP>>() {
            @Override
            public void onResponse(Call<picturebine<ValuesP>> call, Response<picturebine<ValuesP>> response) {
                for (int i = 0; i < response.body().getValues().getTui_brand().size(); i++) {
                    recyclePictureModel item = new recyclePictureModel();
                    item.setPurl(response.body().getValues().getTui_brand().get(i).getUrl());
                    item.setPname(response.body().getValues().getTui_brand().get(i).getCompany_name());
                    item.setId(response.body().getValues().getTui_brand().get(i).getMapping_id());
                    list.add(item);
                }
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<picturebine<ValuesP>> call, Throwable t) {
            }
        });
    }

    private void Amore_pinpai(){
        more_pinpai.setOnClickListener(new View.OnClickListener() {//回调接口
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onItemClick();
                }
            }
        });
        more_news.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.onNewsClick();
                }
            }
        });
    }



    public interface onItemViewClickListener{
        void onItemClick();
        void onNewsClick();
    }




}